package guda.house.biz.impl;

import java.util.List;

import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.enums.SysVarTypeEnum;
import guda.house.biz.query.SysVarQuery;
import guda.house.dal.dao.SysVarDAO;
import guda.house.dal.dataobject.SysVarDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.house.biz.BuildingBiz;
import guda.house.dal.dao.BuildingDAO;
import guda.house.dal.dataobject.BuildingDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class BuildingBizImpl implements BuildingBiz{

    private final static Logger logger = LoggerFactory.getLogger(BuildingBizImpl.class);

    @Autowired
    private BuildingDAO buildingDAO;
    @Autowired
    private SysVarDAO sysVarDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            BuildingDO buildingDO = buildingDAO.getBuildingById(id);
            bizResult.data.put("buildingDO", buildingDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query Building error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = buildingDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<BuildingDO> buildingList = buildingDAO.selectForPage(baseQuery);
            bizResult.data.put("buildingList", buildingList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Building list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            buildingDAO.delBuildingById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete building error", e);
        }
        return bizResult;
    }

    public BizResult create(BuildingDO buildingDO) {
        BizResult bizResult = new BizResult();
        try {
//            SysVarDO sysVarById = sysVarDAO.getSysVarById(buildingDO.getBuildingTypeId());
//            if(sysVarById == null){
//                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
//                return bizResult;
//            }
//            buildingDO.setBuildingType(sysVarById.getVarValue());
            long id = buildingDAO.insert(buildingDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create Building error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(BuildingDO buildingDO) {
        BizResult bizResult = new BizResult();
        try {
//            SysVarDO sysVarById = sysVarDAO.getSysVarById(buildingDO.getBuildingTypeId());
//            if(sysVarById == null){
//                bizResult.code = CommonResultCode.RECORD_NOT_FOUND;
//                return bizResult;
//            }
//            buildingDO.setBuildingType(sysVarById.getVarValue());
            buildingDAO.updateByIdSelective(buildingDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Building error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult queryForCreate(long villageId) {
        BizResult bizResult = new BizResult();



//        SysVarQuery sysVarQuery = new SysVarQuery();
//        sysVarQuery.setVillageId(villageId);
//        sysVarQuery.setVarType(SysVarTypeEnum.building.name());
//        sysVarQuery.setPageSize(500);
//        List<SysVarDO> sysVarDOs = sysVarDAO.selectForPage(sysVarQuery);
//        bizResult.data.put("sysVarList",sysVarDOs);
        bizResult.success = true;
        return bizResult;
    }


}
